<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>第十章：Flask-WTF | Flask进阶课程课件</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./di-shi-yi-zhang-ff1a-cookie-he-session.html" />
    
    
    <link rel="prev" href="./di-ba-zhang-ff1a-flask-migrate.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="10"
        data-chapter-title="第十章：Flask-WTF"
        data-filepath="di-jiu-zhang-ff1a-flask-wtf.md"
        data-basepath="."
        data-revision="Tue Aug 14 2018 17:50:50 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="./chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        第一章：学前准备
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
            
                
                    <a href="./chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        第一节：虚拟环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="di-er-zhang-ff1a-ren-shi-web.html">
            
                
                    <a href="./di-er-zhang-ff1a-ren-shi-web.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        第二章：认识web
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
            
                
                    <a href="./di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        第三章：URL与视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
            
                
                    <a href="./di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        第一节：Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
            
                
                    <a href="./di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        第二节：项目配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
            
                
                    <a href="./di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        第三节：URL与视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
            
                
                    <a href="./di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        第四节：关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        第四章：模版
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        第一节：模版简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        第二节：Jinja2模版概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        第三节：过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        第四节：控制语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        第五节：测试器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        第六节：宏和import语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        第七节：include和set语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        第八节：模版继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        第九节：转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.10.</b>
                        
                        第十节：数据类型和运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
            
                
                    <a href="./di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.11.</b>
                        
                        第十一节：静态文件的配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji.html">
            
                
                    <a href="./di-wu-zhang-ff1a-shi-tu-gao-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        第五章：视图高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
            
                
                    <a href="./di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        第一节：类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
            
                
                    <a href="./di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        第二节：蓝图和子域名
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第六章：SQLAlchemy数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        第一节：MySQL数据库的安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        第二节：SQLAlchemy介绍和基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        第三节：SQLAlchemy的ORM（1）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        第四节：SQLAlchemy的ORM（2）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        第五节：SQLAlchemy的ORM（3）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        第六节：SQLAlchemy的ORM（4）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        第七节：SQLAlchemy的ORM（5）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
            
                
                    <a href="./di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        第八节：Flask-SQLAlchemy
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="di-qi-zhang-ff1a-flask-script.html">
            
                
                    <a href="./di-qi-zhang-ff1a-flask-script.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        第七章：Flask-Script
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="di-bazhang-ff1a-alembic-jiao-cheng.html">
            
                
                    <a href="./di-bazhang-ff1a-alembic-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        第八章：alembic教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="di-ba-zhang-ff1a-flask-migrate.html">
            
                
                    <a href="./di-ba-zhang-ff1a-flask-migrate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        第九章：Flask-Migrate
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="10" data-path="di-jiu-zhang-ff1a-flask-wtf.html">
            
                
                    <a href="./di-jiu-zhang-ff1a-flask-wtf.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        第十章：Flask-WTF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="di-shi-yi-zhang-ff1a-cookie-he-session.html">
            
                
                    <a href="./di-shi-yi-zhang-ff1a-cookie-he-session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        第十一章：cookie和session
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="di-shi-zhang-ff1a-shang-xia-wen.html">
            
                
                    <a href="./di-shi-zhang-ff1a-shang-xia-wen.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        第十二章：Flask上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
            
                
                    <a href="./di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        第十三章：Flask信号机制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="di-shi-si-zhang-ff1a-flask.html">
            
                
                    <a href="./di-shi-si-zhang-ff1a-flask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        第十四章：Flask-Restful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="14.1" data-path="di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
            
                
                    <a href="./di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.1.</b>
                        
                        第一节：Restful API规范
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
            
                
                    <a href="./di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.2.</b>
                        
                        第二节：Flask-Restful插件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="15" data-path="di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
            
                
                    <a href="./di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                        第十五章：memcached教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="16" data-path="di-shi-sizhang-ff1a-redis-jiao-cheng.html">
            
                
                    <a href="./di-shi-sizhang-ff1a-redis-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>16.</b>
                        
                        第十六章：redis教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="17" data-path="di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
            
                
                    <a href="./di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>17.</b>
                        
                        第十七章：CSRF攻击
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="18" data-path="di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
            
                
                    <a href="./di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>18.</b>
                        
                        第十八章：七牛云配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="19" data-path="di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
            
                
                    <a href="./di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>19.</b>
                        
                        第十九章：部署Flask项目
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Flask进阶课程课件</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="flaskwtf">Flask-WTF</h1>
<p><code>Flask-WTF</code>&#x662F;&#x7B80;&#x5316;&#x4E86;<code>WTForms</code>&#x64CD;&#x4F5C;&#x7684;&#x4E00;&#x4E2A;&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#x3002;<code>WTForms</code>&#x8868;&#x5355;&#x7684;&#x4E24;&#x4E2A;&#x4E3B;&#x8981;&#x529F;&#x80FD;&#x662F;&#x9A8C;&#x8BC1;&#x7528;&#x6237;&#x63D0;&#x4EA4;&#x6570;&#x636E;&#x7684;&#x5408;&#x6CD5;&#x6027;&#x4EE5;&#x53CA;&#x6E32;&#x67D3;&#x6A21;&#x677F;&#x3002;&#x5F53;&#x7136;&#x8FD8;&#x5305;&#x62EC;&#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x7684;&#x529F;&#x80FD;&#xFF1A;<code>CSRF&#x4FDD;&#x62A4;</code>&#xFF0C;&#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#x7B49;&#x3002;&#x5B89;&#x88C5;<code>Flask-WTF</code>&#x9ED8;&#x8BA4;&#x4E5F;&#x4F1A;&#x5B89;&#x88C5;<code>WTForms</code>&#xFF0C;&#x56E0;&#x6B64;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x6765;&#x5B89;&#x88C5;<code>Flask-WTF</code>:</p>
<pre><code class="lang-shell">pip install flask-wtf
</code></pre>
<h3 id="&#x8868;&#x5355;&#x9A8C;&#x8BC1;&#xFF1A;">&#x8868;&#x5355;&#x9A8C;&#x8BC1;&#xFF1A;</h3>
<p>&#x5B89;&#x88C5;&#x5B8C;<code>Flask-WTF</code>&#x540E;&#x3002;&#x6765;&#x770B;&#x4E0B;&#x7B2C;&#x4E00;&#x4E2A;&#x529F;&#x80FD;&#xFF0C;&#x5C31;&#x662F;&#x7528;&#x8868;&#x5355;&#x6765;&#x505A;&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#xFF0C;&#x73B0;&#x5728;&#x6709;&#x4E00;&#x4E2A;<code>forms.py</code>&#x6587;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x91CC;&#x9762;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;<code>RegistForm</code>&#x7684;&#x6CE8;&#x518C;&#x9A8C;&#x8BC1;&#x8868;&#x5355;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegistForm</span><span class="hljs-params">(Form)</span>:</span>
    name = StringField(validators=[length(min=<span class="hljs-number">4</span>,max=<span class="hljs-number">25</span>)])
    email = StringField(validators=[email()])
    password = StringField(validators=[DataRequired(),length(min=<span class="hljs-number">6</span>,max=<span class="hljs-number">10</span>),EqualTo(<span class="hljs-string">&apos;confirm&apos;</span>)])
    confirm = StringField()
</code></pre>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x91CC;&#x9762;&#x6307;&#x5B9A;&#x4E86;&#x9700;&#x8981;&#x4E0A;&#x4F20;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5E76;&#x4E14;&#x6307;&#x5B9A;&#x4E86;&#x9A8C;&#x8BC1;&#x5668;&#xFF0C;&#x6BD4;&#x5982;<code>name</code>&#x7684;&#x957F;&#x5EA6;&#x5E94;&#x8BE5;&#x5728;<code>4-25</code>&#x4E4B;&#x95F4;&#x3002;<code>email</code>&#x5FC5;&#x987B;&#x8981;&#x6EE1;&#x8DB3;&#x90AE;&#x7BB1;&#x7684;&#x683C;&#x5F0F;&#x3002;<code>password</code>&#x957F;&#x5EA6;&#x5FC5;&#x987B;&#x5728;<code>6-10</code>&#x4E4B;&#x95F4;&#xFF0C;&#x5E76;&#x4E14;&#x5E94;&#x8BE5;&#x548C;<code>confirm</code>&#x76F8;&#x7B49;&#x624D;&#x80FD;&#x901A;&#x8FC7;&#x9A8C;&#x8BC1;&#x3002;</p>
<p>&#x5199;&#x5B8C;&#x8868;&#x5355;&#x540E;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x662F;<code>regist.html</code>&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;/regist/&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;POST&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">table</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>&#x7528;&#x6237;&#x540D;&#xFF1A;<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;name&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>&#x90AE;&#x7BB1;&#xFF1A;<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;email&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;email&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>&#x5BC6;&#x7801;&#xFF1A;<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;password&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;password&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>&#x786E;&#x8BA4;&#x5BC6;&#x7801;&#xFF1A;<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;password&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;confirm&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">&quot;&#x63D0;&#x4EA4;&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">table</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
</code></pre>
<p>&#x518D;&#x6765;&#x770B;&#x89C6;&#x56FE;&#x51FD;&#x6570;<code>regist</code>&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/regist/&apos;,methods=[&apos;POST&apos;,&apos;GET&apos;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">regist</span><span class="hljs-params">()</span>:</span>
    form = RegistForm(request.form)
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span> <span class="hljs-keyword">and</span> form.validate():
        user = User(name=form.name.data,email=form.email.data,password=form.password.data)
        db.session.add(user)
        db.session.commit()
        <span class="hljs-keyword">return</span> <span class="hljs-string">u&apos;&#x6CE8;&#x518C;&#x6210;&#x529F;!&apos;</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;regist.html&apos;</span>)
</code></pre>
<p><code>RegistForm</code>&#x4F20;&#x9012;&#x7684;&#x662F;<code>request.form</code>&#x8FDB;&#x53BB;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x5E76;&#x4E14;&#x5224;&#x65AD;<code>form.validate</code>&#x4F1A;&#x8FD4;&#x56DE;&#x7528;&#x6237;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;&#x662F;&#x5426;&#x6EE1;&#x8DB3;&#x8868;&#x5355;&#x7684;&#x9A8C;&#x8BC1;&#x3002;</p>
<h3 id="&#x6E32;&#x67D3;&#x6A21;&#x677F;&#xFF1A;">&#x6E32;&#x67D3;&#x6A21;&#x677F;&#xFF1A;</h3>
<p><code>form</code>&#x8FD8;&#x53EF;&#x4EE5;&#x6E32;&#x67D3;&#x6A21;&#x677F;&#xFF0C;&#x8BA9;&#x4F60;&#x5C11;&#x5199;&#x4E86;&#x4E00;&#x4E22;&#x4E22;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6BD4;&#x5982;&#x91CD;&#x5199;&#x4EE5;&#x4E0A;&#x4F8B;&#x5B50;&#xFF0C;<code>RegistForm</code>&#x8868;&#x5355;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegistForm</span><span class="hljs-params">(Form)</span>:</span>
    name = StringField(<span class="hljs-string">u&apos;&#x7528;&#x6237;&#x540D;&#xFF1A;&apos;</span>,validators=[length(min=<span class="hljs-number">4</span>,max=<span class="hljs-number">25</span>)])
    email = StringField(<span class="hljs-string">u&apos;&#x90AE;&#x7BB1;&#xFF1A;&apos;</span>validators=[email()])
    password = StringField(<span class="hljs-string">u&apos;&#x5BC6;&#x7801;&#xFF1A;&apos;</span>,validators=[DataRequired(),length(min=<span class="hljs-number">6</span>,max=<span class="hljs-number">10</span>),EqualTo(<span class="hljs-string">&apos;confirm&apos;</span>)])
    confirm = StringField(<span class="hljs-string">u&apos;&#x786E;&#x8BA4;&#x5BC6;&#x7801;&#xFF1A;&apos;</span>)
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x589E;&#x52A0;&#x4E86;&#x7B2C;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x5728;html&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x505A;&#x6807;&#x7B7E;&#x63D0;&#x793A;&#x4F5C;&#x7528;&#x3002;</p>
<p>&#x5728;<code>app</code>&#x4E2D;&#x7684;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x4FEE;&#x6539;&#x4E3A;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/regist/&apos;,methods=[&apos;POST&apos;,&apos;GET&apos;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">regist</span><span class="hljs-params">()</span>:</span>
    form = RegistForm(request.form)
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span> <span class="hljs-keyword">and</span> form.validate():
        user = User(name=form.name.data,email=form.email.data,password=form.password.data)
        db.session.add(user)
        db.session.commit()
        <span class="hljs-keyword">return</span> <span class="hljs-string">u&apos;&#x6CE8;&#x518C;&#x6210;&#x529F;!&apos;</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;regist.html&apos;</span>,form=form)
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x552F;&#x4E00;&#x7684;&#x4E0D;&#x540C;&#x662F;&#x5728;&#x6E32;&#x67D3;&#x6A21;&#x677F;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x4E86;<code>form</code>&#x8868;&#x5355;&#x53C2;&#x6570;&#x8FDB;&#x53BB;&#xFF0C;&#x8FD9;&#x6837;&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8868;&#x5355;<code>form</code>&#x53D8;&#x91CF;&#x4E86;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x770B;&#x4E0B;<code>regist.html</code>&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;/regist/&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;POST&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">table</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.name.label }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.name() }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.email.label }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.email() }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.password.label }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.password() }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.confirm.label }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>{{ form.confirm() }}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">&quot;&#x63D0;&#x4EA4;&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">table</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
</code></pre>
<h3 id="field&#x5E38;&#x7528;&#x53C2;&#x6570;&#xFF1A;">Field&#x5E38;&#x7528;&#x53C2;&#x6570;&#xFF1A;</h3>
<p>&#x5728;&#x4F7F;&#x7528;<code>Field</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x7ECF;&#x5E38;&#x9700;&#x8981;&#x4F20;&#x9012;&#x4E00;&#x4E9B;&#x53C2;&#x6570;&#x8FDB;&#x53BB;&#xFF0C;&#x4EE5;&#x4E0B;&#x5C06;&#x5BF9;&#x4E00;&#x4E9B;&#x5E38;&#x7528;&#x7684;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x89E3;&#x91CA;&#xFF1A;</p>
<ul>
<li>label&#xFF08;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF09;&#xFF1A;<code>Field</code>&#x7684;label&#x7684;&#x6587;&#x672C;&#x3002;</li>
<li>validators&#xFF1A;&#x9A8C;&#x8BC1;&#x5668;&#x3002;</li>
<li>id&#xFF1A;<code>Field</code>&#x7684;id&#x5C5E;&#x6027;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E0D;&#x5199;&#x4E3A;&#x8BE5;&#x5C5E;&#x6027;&#x540D;&#x3002;</li>
<li>default&#xFF1A;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</li>
<li>widget&#xFF1A;&#x6307;&#x5B9A;&#x7684;<code>html</code>&#x63A7;&#x4EF6;&#x3002;</li>
</ul>
<h3 id="&#x5E38;&#x7528;field&#xFF1A;">&#x5E38;&#x7528;Field&#xFF1A;</h3>
<ul>
<li>BooleanField&#xFF1A;&#x5E03;&#x5C14;&#x7C7B;&#x578B;&#x7684;Field&#xFF0C;&#x6E32;&#x67D3;&#x51FA;&#x53BB;&#x662F;<code>checkbox</code>&#x3002;</li>
<li><p>FileField&#xFF1A;&#x6587;&#x4EF6;&#x4E0A;&#x4F20;Field&#x3002;</p>
<pre><code class="lang-python">  <span class="hljs-comment"># forms.py</span>
  <span class="hljs-keyword">from</span> flask_wtf.file <span class="hljs-keyword">import</span> FileField,FileAllowed,FileRequired
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UploadForm</span><span class="hljs-params">(FlaskForm)</span>:</span>
      avatar = FileField(<span class="hljs-string">u&apos;&#x5934;&#x50CF;&#xFF1A;&apos;</span>,validators=[FileRequired(),FileAllowed([])])

  <span class="hljs-comment"># app.py</span>
<span class="hljs-decorator">  @app.route(&apos;/profile/&apos;,methods=(&apos;POST&apos;,&apos;GET&apos;))</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">profile</span><span class="hljs-params">()</span>:</span>
      form = ProfileForm()
      <span class="hljs-keyword">if</span> form.validate_on_submit():
          filename = secure_filename(form.avatar.data.filename)
          form.avatar.data.save(os.path.join(app.config[<span class="hljs-string">&apos;UPLOAD_FOLDER&apos;</span>],filename))
      <span class="hljs-keyword">return</span> <span class="hljs-string">u&apos;&#x4E0A;&#x4F20;&#x6210;&#x529F;&apos;</span>

      <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;profile.html&apos;</span>,form=form)
</code></pre>
</li>
<li><p>FloatField&#xFF1A;&#x6D6E;&#x70B9;&#x6570;&#x7C7B;&#x578B;&#x7684;Field&#xFF0C;&#x4F46;&#x662F;&#x6E32;&#x67D3;&#x51FA;&#x53BB;&#x7684;&#x65F6;&#x5019;&#x662F;<code>text</code>&#x7684;input&#x3002;</p>
</li>
<li><p>IntegerField&#xFF1A;&#x6574;&#x5F62;&#x7684;Field&#x3002;&#x540C;FloatField&#x3002;</p>
</li>
<li><p>RadioField&#xFF1A;<code>radio</code>&#x7C7B;&#x578B;&#x7684;<code>input</code>&#x3002;&#x8868;&#x5355;&#x4F8B;&#x5B50;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-comment"># form.py</span>
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegistrationForm</span><span class="hljs-params">(FlaskForm)</span>:</span>
      gender = wtforms.RadioField(<span class="hljs-string">u&apos;&#x6027;&#x522B;&#xFF1A;&apos;</span>,validators=[DataRequired()])
</code></pre>
<p>&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>
          {{ form.gender.label }}
      <span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>
          {% for gender in form.gender %}
              {{ gender.label }}
              {{ gender }}
          {% endfor %}
      <span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span>
</code></pre>
<p><code>app.py</code>&#x6587;&#x4EF6;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF0C;&#x7ED9;<code>gender</code>&#x6DFB;&#x52A0;&#x4E86;<code>choices</code>&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">  @app.route(&apos;/register/&apos;,methods=[&apos;POST&apos;,&apos;GET&apos;])</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">register</span><span class="hljs-params">()</span>:</span>
      form = RegistrationForm()
      form.gender.choices = [(<span class="hljs-string">&apos;1&apos;</span>,<span class="hljs-string">u&apos;&#x7537;&apos;</span>),(<span class="hljs-string">&apos;2&apos;</span>,<span class="hljs-string">u&apos;&#x5973;&apos;</span>)]
      <span class="hljs-keyword">if</span> form.validate_on_submit():
          <span class="hljs-keyword">return</span> <span class="hljs-string">u&apos;success&apos;</span>

      <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;register.html&apos;</span>,form=form)
</code></pre>
</li>
<li><p>SelectField&#xFF1A;&#x7C7B;&#x4F3C;&#x4E8E;<code>RadioField</code>&#x3002;&#x770B;&#x4EE5;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-comment"># forms.py</span>
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProfileForm</span><span class="hljs-params">(FlaskForm)</span>:</span>
      language = wtforms.SelectField(<span class="hljs-string">&apos;Programming Language&apos;</span>,choices=[(<span class="hljs-string">&apos;cpp&apos;</span>,<span class="hljs-string">&apos;C++&apos;</span>),(<span class="hljs-string">&apos;py&apos;</span>,<span class="hljs-string">&apos;python&apos;</span>),(<span class="hljs-string">&apos;text&apos;</span>,<span class="hljs-string">&apos;Plain Text&apos;</span>)],validators=[DataRequired()])
</code></pre>
<p>&#x518D;&#x6765;&#x770B;<code>app.py</code>&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">  @app.route(&apos;/profile/&apos;,methods=(&apos;POST&apos;,&apos;GET&apos;))</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">profile</span><span class="hljs-params">()</span>:</span>
      form = ProfileForm()
      <span class="hljs-keyword">if</span> form.validate_on_submit():
          <span class="hljs-keyword">print</span> form.language.data
          <span class="hljs-keyword">return</span> <span class="hljs-string">u&apos;&#x4E0A;&#x4F20;&#x6210;&#x529F;&apos;</span>
      <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;profile.html&apos;</span>,form=form)
</code></pre>
<p>&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;/profile/&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;POST&quot;</span>&gt;</span>
      {{ form.csrf_token }}
      {{ form.language.label }}
      {{ form.language() }}
      <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
</code></pre>
</li>
<li><p>StringField&#xFF1A;&#x6E32;&#x67D3;&#x5230;&#x6A21;&#x677F;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x4E3A;<code>&lt;input type=&apos;text&apos;&gt;</code>&#xFF0C;&#x5E76;&#x4E14;&#x662F;&#x6700;&#x57FA;&#x672C;&#x7684;&#x6587;&#x672C;&#x9A8C;&#x8BC1;&#x3002;</p>
</li>
<li><p>PasswordField&#xFF1A;&#x6E32;&#x67D3;&#x51FA;&#x6765;&#x7684;&#x662F;&#x4E00;&#x4E2A;<code>password</code>&#x7684;<code>input</code>&#x6807;&#x7B7E;&#x3002;</p>
</li>
<li><p>TextAreaField&#xFF1A;&#x6E32;&#x67D3;&#x51FA;&#x6765;&#x7684;&#x662F;&#x4E00;&#x4E2A;<code>textarea</code>&#x3002;</p>
</li>
</ul>
<h3 id="&#x5E38;&#x7528;&#x7684;&#x9A8C;&#x8BC1;&#x5668;&#xFF1A;">&#x5E38;&#x7528;&#x7684;&#x9A8C;&#x8BC1;&#x5668;&#xFF1A;</h3>
<p>&#x6570;&#x636E;&#x53D1;&#x9001;&#x8FC7;&#x6765;&#xFF0C;&#x7ECF;&#x8FC7;&#x8868;&#x5355;&#x9A8C;&#x8BC1;&#xFF0C;&#x56E0;&#x6B64;&#x9700;&#x8981;&#x9A8C;&#x8BC1;&#x5668;&#x6765;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#xFF0C;&#x4EE5;&#x4E0B;&#x5BF9;&#x4E00;&#x4E9B;&#x5E38;&#x7528;&#x7684;&#x5185;&#x7F6E;&#x9A8C;&#x8BC1;&#x5668;&#x8FDB;&#x884C;&#x8BB2;&#x89E3;&#xFF1A;</p>
<ul>
<li>Email&#xFF1A;&#x9A8C;&#x8BC1;&#x4E0A;&#x4F20;&#x7684;&#x6570;&#x636E;&#x662F;&#x5426;&#x4E3A;&#x90AE;&#x7BB1;&#x3002;</li>
<li>EqualTo&#xFF1A;&#x9A8C;&#x8BC1;&#x4E0A;&#x4F20;&#x7684;&#x6570;&#x636E;&#x662F;&#x5426;&#x548C;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x76F8;&#x7B49;&#xFF0C;&#x5E38;&#x7528;&#x7684;&#x5C31;&#x662F;&#x5BC6;&#x7801;&#x548C;&#x786E;&#x8BA4;&#x5BC6;&#x7801;&#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x3002;</li>
<li>InputRequired&#xFF1A;&#x539F;&#x59CB;&#x6570;&#x636E;&#x7684;&#x9700;&#x8981;&#x9A8C;&#x8BC1;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#xFF0C;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;<code>InputRequired</code>&#x3002;</li>
<li>Length&#xFF1A;&#x957F;&#x5EA6;&#x9650;&#x5236;&#xFF0C;&#x6709;min&#x548C;max&#x4E24;&#x4E2A;&#x503C;&#x8FDB;&#x884C;&#x9650;&#x5236;&#x3002;</li>
<li>NumberRange&#xFF1A;&#x6570;&#x5B57;&#x7684;&#x533A;&#x95F4;&#xFF0C;&#x6709;min&#x548C;max&#x4E24;&#x4E2A;&#x503C;&#x9650;&#x5236;&#xFF0C;&#x5982;&#x679C;&#x5904;&#x5728;&#x8FD9;&#x4E24;&#x4E2A;&#x6570;&#x5B57;&#x4E4B;&#x95F4;&#x5219;&#x6EE1;&#x8DB3;&#x3002;</li>
<li>Regexp&#xFF1A;&#x81EA;&#x5B9A;&#x4E49;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</li>
<li>URL&#xFF1A;&#x5FC5;&#x987B;&#x8981;&#x662F;<code>URL</code>&#x7684;&#x5F62;&#x5F0F;&#x3002;</li>
<li>UUID&#xFF1A;&#x9A8C;&#x8BC1;<code>UUID</code>&#x3002;</li>
</ul>
<h3 id="&#x81EA;&#x5B9A;&#x4E49;&#x9A8C;&#x8BC1;&#x5B57;&#x6BB5;&#xFF1A;">&#x81EA;&#x5B9A;&#x4E49;&#x9A8C;&#x8BC1;&#x5B57;&#x6BB5;&#xFF1A;</h3>
<p>&#x4F7F;&#x7528;<code>validate_fieldname(self,field)</code>&#x53EF;&#x4EE5;&#x5BF9;&#x67D0;&#x4E2A;&#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x66F4;&#x52A0;&#x8BE6;&#x7EC6;&#x7684;&#x9A8C;&#x8BC1;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProfileForm</span><span class="hljs-params">(FlaskForm)</span>:</span>
    name = wtforms.StringField(<span class="hljs-string">&apos;name&apos;</span>,[validators.InputRequired()])
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">validate_name</span><span class="hljs-params">(self,field)</span>:</span>
        <span class="hljs-keyword">if</span> len(field.data) &gt; <span class="hljs-number">5</span>:
            <span class="hljs-keyword">raise</span> wtforms.ValidationError(<span class="hljs-string">u&apos;&#x8D85;&#x8FC7;5&#x4E2A;&#x5B57;&#x7B26;&apos;</span>)
</code></pre>
<h3 id="csrf&#x4FDD;&#x62A4;&#xFF1A;">CSRF&#x4FDD;&#x62A4;&#xFF1A;</h3>
<p>&#x5728;flask&#x7684;&#x8868;&#x5355;&#x4E2D;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;&#x5F00;&#x542F;&#x4E86;<code>csrf</code>&#x4FDD;&#x62A4;&#x529F;&#x80FD;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5173;&#x95ED;&#x8868;&#x5355;&#x7684;<code>csrf</code>&#x4FDD;&#x62A4;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x521D;&#x59CB;&#x5316;&#x8868;&#x5355;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x9012;<code>csrf_enabled=False</code>&#x8FDB;&#x53BB;&#x6765;&#x5173;&#x95ED;<code>csrf</code>&#x4FDD;&#x62A4;&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5173;&#x95ED;&#x8FD9;&#x79CD;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5728;&#x6CA1;&#x6709;&#x8868;&#x5355;&#x5B58;&#x5728;&#x7684;&#x8BF7;&#x6C42;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x4E5F;&#x6DFB;&#x52A0;<code>csrf</code>&#x4FDD;&#x62A4;&#xFF0C;&#x53EF;&#x4EE5;&#x5F00;&#x542F;&#x5168;&#x5C40;&#x7684;<code>csrf</code>&#x4FDD;&#x62A4;&#x529F;&#x80FD;&#xFF1A;</p>
<pre><code class="lang-python">csrf = CsrfProtect()
csrf.init_app(app)
</code></pre>
<p>&#x6216;&#x8005;&#x662F;&#x9488;&#x5BF9;&#x67D0;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF0C;&#x4F7F;&#x7528;<code>csrf.protect</code>&#x88C5;&#x9970;&#x5668;&#x6765;&#x5F00;&#x542F;<code>csrf</code>&#x4FDD;&#x62A4;&#x529F;&#x80FD;&#x3002;&#x5E76;&#x4E14;&#x5982;&#x679C;&#x5DF2;&#x7ECF;&#x5F00;&#x542F;&#x4E86;&#x5168;&#x5C40;&#x7684;<code>csrf</code>&#x4FDD;&#x62A4;&#xFF0C;&#x60F3;&#x8981;&#x5173;&#x95ED;&#x67D0;&#x4E2A;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;<code>csrf</code>&#x4FDD;&#x62A4;&#x529F;&#x80FD;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>csrf.exempt</code>&#x88C5;&#x9970;&#x5668;&#x6765;&#x53D6;&#x6D88;&#x672C;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x4FDD;&#x62A4;&#x529F;&#x80FD;&#x3002;</p>
<h3 id="ajax&#x7684;csrf&#x4FDD;&#x62A4;&#xFF1A;">AJAX&#x7684;CSRF&#x4FDD;&#x62A4;&#xFF1A;</h3>
<p>&#x5728;<code>AJAX</code>&#x4E2D;&#x8981;&#x4F7F;&#x7528;<code>csrf</code>&#x4FDD;&#x62A4;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x624B;&#x52A8;&#x7684;&#x6DFB;&#x52A0;<code>X-CSRFToken</code>&#x5230;<code>Header</code>&#x4E2D;&#x3002;&#x4F46;&#x662F;<code>CSRF</code>&#x4ECE;&#x54EA;&#x91CC;&#x6765;&#xFF0C;&#x8FD8;&#x662F;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x6A21;&#x677F;&#x7ED9;&#x6E32;&#x67D3;&#xFF0C;&#x800C;<code>Flask</code>&#x6BD4;&#x8F83;&#x63A8;&#x8350;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x5728;<code>meta</code>&#x6807;&#x7B7E;&#x4E2D;&#x6E32;&#x67D3;<code>csrf</code>&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;csrf-token&quot;</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">&quot;{{ csrf_token() }}&quot;</span>&gt;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x53D1;&#x9001;<code>AJAX</code>&#x8BF7;&#x6C42;&#xFF0C;&#x5219;&#x5728;&#x53D1;&#x9001;&#x4E4B;&#x524D;&#x8981;&#x6DFB;&#x52A0;<code>CSRF</code>,&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF08;&#x4F7F;&#x7528;&#x4E86;jQuery&#xFF09;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> csrftoken = $(<span class="hljs-string">&apos;meta[name=csrf-token]&apos;</span>).attr(<span class="hljs-string">&apos;content&apos;</span>)
$.ajaxSetup({
    beforeSend: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">xhr, settings</span>) </span>{
        <span class="hljs-keyword">if</span> (!<span class="hljs-regexp">/^(GET|HEAD|OPTIONS|TRACE)$/i</span>.test(settings.type) &amp;&amp; !<span class="hljs-keyword">this</span>.crossDomain) {
            xhr.setRequestHeader(<span class="hljs-string">&quot;X-CSRFToken&quot;</span>, csrftoken)
        }
    }
})
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./di-ba-zhang-ff1a-flask-migrate.html" class="navigation navigation-prev " aria-label="Previous page: 第九章：Flask-Migrate"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./di-shi-yi-zhang-ff1a-cookie-he-session.html" class="navigation navigation-next " aria-label="Next page: 第十一章：cookie和session"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
